package com.arron.algorithm.leetcodetop100.字符串;


/**
 *  字符串相乘，相加
 *  https://leetcode.cn/problems/multiply-strings/solutions/29100/you-hua-ban-shu-shi-da-bai-994-by-breezean/
 */
public class 字符串相乘 {

    public String multiply(String num1, String num2) {

        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }


        int m = num1.length();
        int n = num2.length();
        char[] chars1 = num1.toCharArray();
        char[] chars2 = num2.toCharArray();

        // 使用数组记录相乘的结果 两数相乘，位数不可能超过m+n
        int[] res = new int[m+n];

        // 使用双指针 i,j分别指向 字符串mus1，num2的末尾
        for (int i = m-1; i >= 0; i--) {
            for (int j = n-1; j >=0 ; j--) {

                int multiplyNum = (chars2[j]-'0')*(chars1[i]-'0');

                System.out.println("multiplyNum ="+multiplyNum);

                int p1 = i+j;
                int p2 = i+j+1;


                int sum = multiplyNum + res[p2];

                // 这里需要注意 ， 如果这里有进位，会在下一次for循环处理，也可以单独处理，但是没必要
                res[p1] += sum/10;

                res[p2] = sum%10;

            }

        }

        int i = 0;
        for (; i < res.length; i++) {
            if (res[i] !=0){
                break;
            }
        }

        StringBuffer sb = new StringBuffer();

        for (int j = i; j < res.length; j++) {
            sb.append(res[j]);
        }


        return sb.toString();

    }


    public static void main(String[] args) {
        int a = 8;
        int b = 10;
        System.out.println(a%b);
    }

}

